﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using BanaToys.InfoSys.Bll;
using BanaToys.InfoSys.Bol;
using Newtonsoft.Json;

namespace BanaToys.InfoSys.Web.ApiCall.BanaWww
{
    /// <summary>
    /// API调用者
    /// </summary>
    public class Upload_ApiCall
    {
        private const string Url_Upload = "v30/upload/upload_file.ashx";

        #region 1.单个文件上传

        /// <summary>
        /// 单个文件上传
        /// </summary>
        /// <param name="formdata"></param>
        /// <param name="uploadResponse"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool UploadFile(i_upload_formdata formdata,out a_upload uploadResponse,out string message)
        {
            //
            uploadResponse = null;
            message = null;
            if (formdata == null)
            {
                message = "待上传文件及表单对象为空";
                return false;
            }

            //判断本机物理文件是否存在
            string file_pathname = formdata.file_physics_path_name;
            if (!File.Exists(file_pathname))
            {
                message = "待上传文件不存在";
                return false;
            }

            //调用接口
            string url_full = API_Config.API_Url.TrimEnd('/') + "/" + Url_Upload.TrimStart('/');

            //header
            Dictionary<string, string> dictHeader = API_Config.GetHeaderAuth();

            //body
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("module", formdata.module);
            nvc.Add("pkid", formdata.pkid);
            nvc.Add("thumb", formdata.thumb.ToString());

            //files
            string[] files = new[] {file_pathname};

            //
            string json;
            string errorText;
            //bool ok = HttpHelp.PostFunction(url_full, para_json, dictHeader, "POST", out json, out errorText);
            bool ok = HttpHelp.UploadMedia_v202303(url_full, dictHeader, nvc, files, out json, out errorText);
            if (!ok)
            {
                message = "错误:" + errorText;
                return false;
            }

            ZBug.Log(json);

            //解析JSON
            //i_upload_a upload_msg = null;
            try
            {
                uploadResponse = JsonConvert.DeserializeObject<a_upload>(json);
            }
            catch (Exception ex)
            {
                message = "解析服务端返回JSON失败:" + ex.Message;
                return false;
            }

            if (uploadResponse == null)
            {
                message = "服务端返回空";
                return false;
            }

            //业务判断
            message = string.Format("{0}", uploadResponse.errmsg);
            if (uploadResponse.errcode != 0)
            {
                message = string.Format("api返回失败:{0}({1})", uploadResponse.errmsg, uploadResponse.errcode);
                return false;
            }

            //返回
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        private static void Test_UploadFile()
        {
            i_upload_formdata data = new i_upload_formdata(); //test
            data.thumb = true;
            data.pkid = "997040003";
            data.module = "Product";
            data.file_physics_path_name = @"E:\Dev_Temp\database_pic\997\99704\997040003.jpg";

            //
            a_upload a;
            string message;
            bool ok = UploadFile(data, out a, out message);
            //Debug.WriteLine(string.Format("success={0},message={1}", ok, message));
            ZBug.Log(string.Format("success={0},message={1}", ok, message));
            if (a != null)
            {
                string s = Help.ObjectToString(a);
                //Debug.WriteLine(s);
                ZBug.Log(s);
            }
            else
            {
                //Debug.WriteLine("a is null");
                ZBug.Log("a is null");
            }

            /*
file_name=992090070.jpg
uploaded_url=product/2023_03/992090070__20230308_100540971.jpg
index=0
thumb=True
FileSizeByte=35122
FileMD5=1749e5c17dc6ad6f76840b81114929e3
FileOriginalName=992090070.jpg
errcode=0
errmsg=ok
time=2023-03-08T10:05:41+08:00             
             */

            /*
file_name=997040003.jpg
uploaded_url=product/2023_03/997040003__20230308_101854669.jpg
index=0
thumb=True
FileSizeByte=88731
FileMD5=f636bd5cd3ffff29cdba6ef89b3e3b5a
FileOriginalName=997040003.jpg
errcode=0
errmsg=ok
time=2023-03-08T10:18:54+08:00
             */

        }

        #endregion

        #region 2.多个文件上传
        //TODO:
        #endregion

    }
}